from abc import ABC, abstractmethod
from django.utils import timezone
from ..models import CrawlerTask, CrawledData

class BaseCrawler(ABC):
    """爬虫基类"""
    
    def __init__(self, task):
        self.task = task
        self.total_items = 0
        self.crawled_items = 0
    
    def update_progress(self, progress, error_message=None):
        """更新任务进度"""
        self.task.progress = progress
        if error_message:
            self.task.error_message = error_message
        self.task.save()
    
    def save_data(self, data, metadata=None):
        """保存爬取的数据"""
        CrawledData.objects.create(
            task=self.task,
            data=data,
            metadata=metadata or {}
        )
        self.crawled_items += 1
        self.task.crawled_items = self.crawled_items
        self.task.save()
    
    def complete_task(self, success=True):
        """完成任务"""
        self.task.status = (
            CrawlerTask.TaskStatus.COMPLETED if success 
            else CrawlerTask.TaskStatus.FAILED
        )
        self.task.completed_at = timezone.now()
        self.task.save()
    
    @abstractmethod
    def start(self):
        """开始爬取数据"""
        pass
    
    @abstractmethod
    def stop(self):
        """停止爬取数据"""
        pass
    
    @abstractmethod
    def validate_credentials(self):
        """验证数据源凭证"""
        pass 